---
title: "Summary Tables Appendix"
author: "Omar Hammoud Gallego, Roberto Foa, Xavier Romero-Vidal"
date: "12/07/2022"
output: 
  pdf_document:
    keep_tex: true
    latex_engine: xelatex

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
	echo = FALSE,
	message = FALSE,
	warning = FALSE
)
```

```{r setup and upload packages, include=FALSE, results = FALSE}
library(tm)
library(tidytext)
library(dplyr)
library(SnowballC)
library(ggplot2)
library(textdata)
library(topicmodels)
library("tidyverse")
#install.packages("Twitmo")
library("Twitmo")

library(scales)
library(quanteda)

library(sjlabelled)

#install.packages("reshape")
library(reshape)
library(tidyverse)
library(gdata)
#install.packages("aod")
library("aod")

#install.packages("DataCombine")
library("DataCombine")
library(lubridate)

#install.packages("srvyr")
library(srvyr)

#install.packages("dotwhisker")
library("dotwhisker")
library(lme4)

#install.packages("parameters")  # to calculate ci_method="wald"
library("parameters")

#install.packages("stm")
library("stm")

#install.packages("see")  # Set of packages to visualise models
library("see")
#rm(list=ls()) to clean environment

# packages for multi level model
library(cowplot)
library(haven)
library(lme4)
library(car)
library(arm)
library(dplyr)
library(MuMIn)
library(lmerTest)
library(estimatr)
library("foreign")
library(stargazer)
library(jtools)


library("naniar")
library(gtsummary)
library(kableExtra)

```


```{r, YouGov Summary Statistics Upload Data}

# Omar Windows upload. 
#setwd("C:/Users/omarh/Documents/GitHub/Satisfaction_Democracy/data-raw/You_Gov/")

setwd("/Users/HammoudG/Documents/GitHub/Satisfaction_Democracy/data-raw/You_Gov/")

#You_Gov<- read.csv("You_Gov_Dec_2021.csv")



You_Gov_1 <- read.spss("JP_dematts_GB_tracker_Oct19.sav", to.data.frame = TRUE) 

You_Gov_2 <- read.spss("JP_dematts_GB_tracker_Oct19_J.sav", to.data.frame = TRUE) 

# Bind two datasets
You_Gov_Final<- bind_rows(You_Gov_1, You_Gov_2)

# Remove duplicated rows based on Sepal.Length
You_Gov_Final<- You_Gov_Final %>% distinct(caseid, .keep_all = TRUE)
  
  
```

```{r, summary table}


You_Gov_Final_Table<-You_Gov_Final %>% 
  select(CAM_demtrack_satisfied, pastvote_EURef, profile_GOR, voted_ge_2019,pastvote_ge_2019) %>%
  tbl_summary(missing = "ifany", missing_text = "(Missing)") %>%
 #tbl_summary(by = country, missing = "ifany", missing_text = "(Missing)") %>% 
 # add_p() %>% 
  bold_labels() %>%
 # add_n() %>%
  #add_overall() %>%
   modify_spanning_header(all_stat_cols() ~ "**Summary Statistics, YouGov Tracker**") %>%
  as_kable_extra(format = "latex")

You_Gov_Final_Table %>% kableExtra::kable_styling(latex_options= c("scale_down", "HOLD_position")) %>% column_spec(1, bold = T, border_right = T)




You_Gov_Final_Table<-You_Gov_Final %>% 
  select(voted2017, pastvote_2017, profile_gross_household, age, profile_gender) %>%
  tbl_summary(missing = "ifany", missing_text = "(Missing)") %>%
 #tbl_summary(by = country, missing = "ifany", missing_text = "(Missing)") %>% 
 # add_p() %>% 
  bold_labels() %>%
 # add_n() %>%
  #add_overall() %>%
   modify_spanning_header(all_stat_cols() ~ "**Summary Statistics, YouGov Tracker**") %>%
  as_kable_extra(format = "latex")

You_Gov_Final_Table %>% kableExtra::kable_styling(latex_options= c("scale_down", "HOLD_position")) %>% column_spec(1, bold = T, border_right = T)




You_Gov_Final_Table<-You_Gov_Final %>% 
  select(profile_education_level, political_attention, profile_newspaper_readership) %>%
  tbl_summary(missing = "ifany", missing_text = "(Missing)") %>%
 #tbl_summary(by = country, missing = "ifany", missing_text = "(Missing)") %>% 
 # add_p() %>% 
  bold_labels() %>%
 # add_n() %>%
  #add_overall() %>%
   modify_spanning_header(all_stat_cols() ~ "**Summary Statistics, YouGov Tracker**") %>%
  as_kable_extra(format = "latex")

You_Gov_Final_Table %>% kableExtra::kable_styling(latex_options= c("scale_down", "HOLD_position")) %>% column_spec(1, bold = T, border_right = T)
```


```{r, Tweets summary upload}

setwd("/Users/HammoudG/OneDrive - London School of Economics/Satisfaction_Democracy_Project/")

# Omar Windows upload. 
#setwd("C:/Users/omarh/OneDrive - London School of Economics/Satisfaction_Democracy_Project")

Tweets<- read.csv("All_Tweets_Classified_Sentiment.csv", stringsAsFactors = TRUE)


```


```{r, Tweets summary table}


Tweets_Table<-Tweets %>% 
  select(newspaper, topic) %>%
  tbl_summary(missing = "ifany", missing_text = "(Missing)") %>%
 #tbl_summary(by = country, missing = "ifany", missing_text = "(Missing)") %>% 
 # add_p() %>% 
  bold_labels() %>%
 # add_n() %>%
 # add_overall() %>%
   modify_spanning_header(all_stat_cols() ~ "**Summary Statistics, Twitter Data**") %>%
  as_kable_extra(format = "latex")

Tweets_Table %>% kableExtra::kable_styling(latex_options= c("scale_down", "HOLD_position")) %>% column_spec(1, bold = T, border_right = T)



# SECOND Summary table of public_metrics.retweet_count and sentiment_value

#Tweets$created_at.x<- as.Date(as.character(Tweets$created_at.x))
#class(Tweets$created_at.x)

Tweets %>%
    select(public_metrics.retweet_count, sentiment_value, created_at.x) %>%
    stargazer(title= "Summary Statistics, Twitter Data")



```